/*
 * Copyright 2016 Stuart Kent
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License.
 *
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package com.example.mybugshaker.flow.dialog;

import com.example.mybugshaker.ResourceTable;
import ohos.aafwk.ability.Ability;
import ohos.agp.window.dialog.IDialog;
import ohos.agp.window.dialog.ToastDialog;
import ohos.global.resource.NotExistException;
import ohos.global.resource.ResourceManager;
import ohos.global.resource.WrongTypeException;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;

public class AppCompatDialogProvider implements DialogProvider {
    private final HiLogLabel lable = new HiLogLabel(HiLog.LOG_APP, 0x00201, "AppCompatDialogProvider");

    @NotNull
    @Override
    public final ToastDialog getAlertDialog(
            @NotNull final Ability ability,
            @NotNull  final IDialog.ClickedListener reportBugClickListener) {

        ResourceManager resourceManager;
        resourceManager = ability.getContext().getResourceManager();
        try {
            ToastDialog toastDialog = new ToastDialog(ability.getContext());

            toastDialog
                    .setTitleText(resourceManager.getElement(ResourceTable.String_bugshaker_dialog_title).getString())
                    .setContentText(resourceManager.getElement(ResourceTable.String_bugshaker_dialog_message).getString())
                    .setButton(1, resourceManager.getElement(ResourceTable.String_bugshaker_dialog_positive_action).getString(), reportBugClickListener)
                    .setButton(1, resourceManager.getElement(ResourceTable.String_bugshaker_dialog_negative_action).getString(), null)
                    .setAutoClosable(false)
                    .show();
            return toastDialog;
        } catch (IOException e) {
            HiLog.error(lable,"AppCompatDialogProvider", e);
        } catch (NotExistException e) {
            HiLog.error(lable,"AppCompatDialogProvider", e);
        } catch (WrongTypeException e) {
            HiLog.error(lable,"AppCompatDialogProvider", e);
        }

        return null;
    }
}
